import { ProFormSwitch, ProFormText } from '@ant-design/pro-form';

export default (props: { isPersonal: boolean }) => {
  const { isPersonal } = props;

  const newLabel = isPersonal ? '新密码' : '密码';

  return (
    <>
      {isPersonal && (
        <ProFormText.Password
          label="旧密码"
          name="password"
          rules={[{ required: true, message: '请填写旧密码' }]}
        />
      )}
      <ProFormText.Password
        label={newLabel}
        name="new_password"
        rules={[{ required: true, message: `请填写${newLabel}` }]}
      />
      <ProFormText.Password
        label="确认密码"
        name="confirm_password"
        dependencies={['new_password']}
        rules={[
          { required: true, message: '请填写确认密码' },
          ({ getFieldValue }) => ({
            validator(_, value) {
              if (!value || getFieldValue('new_password') === value) {
                return Promise.resolve();
              }
              return Promise.reject(new Error('两次密码不一致'));
            },
          }),
        ]}
      />
      {!isPersonal && <ProFormSwitch name="need_renew" label="是否为临时密码" />}
    </>
  );
};
